#include "stdafx.h"
#include "QActionDefines.h"


ActionBase::ActionBase()
{
}

ActionBase::~ActionBase()
{
}

ActionType ActionBase::GetType()
{
    return m_MsgType;
}

void ActionBase::SetType(ActionType type)
{
    m_MsgType = type;
}

QString ActionBase::GetErrMsg()
{
    return m_ErrMsg;
}

void ActionBase::SetErrMsg(QString const& errMsg)
{
    m_ErrMsg = errMsg;
}

bool ActionBase::IsSuccess()
{
    return m_IsSuccess;
}

void ActionBase::SetSuccess(bool isSuccess)
{
    m_IsSuccess = isSuccess;
}

void ActionBase::SetSock(QTcpSocket* pSock)
{
    m_pSock = pSock;
}

QTcpSocket* ActionBase::GetSock()
{
    return m_pSock;
}

void ActionBase::InFieldAction(QJsonObject rootObj)
{
    m_MsgType = (ActionType)rootObj["action"].toInt();
    m_IsSuccess = rootObj["issuccess"].toBool();
    m_ErrMsg = rootObj["errmsg"].toString();
}

QJsonObject ActionBase::OutFieldAction()
{
    QJsonObject rootObject;
    rootObject.insert("action", (int)m_MsgType);
    rootObject.insert("issuccess", m_IsSuccess);
    rootObject.insert("errmsg", m_ErrMsg);
    return rootObject;
}

LoginAction::LoginAction()
{
}

LoginAction::~LoginAction()
{
}

QString LoginAction::GetUserName()
{
    return m_UserName;
}

void LoginAction::SetUserName(QString const& userName)
{
    m_UserName = userName;
}

void LoginAction::InFieldAction(QJsonObject rootObj)
{
    ActionBase::InFieldAction(rootObj);

    QJsonObject dataObject = rootObj["logindata"].toObject();
    m_UserName = dataObject["name"].toString();
    m_Token = dataObject["token"].toString();
}

QJsonObject LoginAction::OutFieldAction()
{
    QJsonObject rootObject = ActionBase::OutFieldAction();

    QJsonObject dataObject;
    dataObject.insert("name", m_UserName);
    dataObject.insert("token", m_Token);
    rootObject.insert("logindata", dataObject);

    return rootObject;
}

QString LoginAction::GetToken()
{
    return m_Token;
}

void LoginAction::SetToken(QString const& token)
{
    m_Token = token;
}

GetUserListAction::GetUserListAction()
{
}

GetUserListAction::~GetUserListAction()
{
}

void GetUserListAction::InFieldAction(QJsonObject rootObj)
{
    LoginAction::InFieldAction(rootObj);

    QJsonArray dataObject = rootObj["getuserlistdata"].toArray();

    for (int i = 0; i < dataObject.size(); i++)
    {
        QJsonValue userName = dataObject.at(i);
        m_UserInfoList.append({ userName.toString() });
    }
}

QJsonObject GetUserListAction::OutFieldAction()
{
    QJsonObject rootObject = LoginAction::OutFieldAction();

    QJsonArray dataObject;
    for (auto item : m_UserInfoList)
    {
        dataObject.append(item.userName);
    }

    rootObject.insert("getuserlistdata", dataObject);

    return rootObject;
}

UserMessageAction::UserMessageAction()
{
}

UserMessageAction::~UserMessageAction()
{
}

UserMessageType UserMessageAction::GetMessageType()
{
    return m_UserMessageType;
}

void UserMessageAction::SetMessageType(UserMessageType type)
{
    m_UserMessageType = type;
}

void UserMessageAction::SetPlanMessage(QString const& msg)
{
    m_UserMessageType = UserMessageType::kPlanMsgType;
    m_UserMsg = msg;
}

QString UserMessageAction::GetPlanMessage()
{
    return m_UserMsg;
}

void UserMessageAction::InFieldAction(QJsonObject rootObj)
{
    LoginAction::InFieldAction(rootObj);

    QJsonObject dataObject = rootObj["MessageData"].toObject();

    QByteArray byteArr = dataObject["UserMsg"].toString().toStdString().c_str();
    m_UserMsg = byteArr.fromBase64(byteArr);
    m_UserMessageType = (UserMessageType)dataObject["UserMessageType"].toInt();
}

QJsonObject UserMessageAction::OutFieldAction()
{
    QJsonObject rootObject = LoginAction::OutFieldAction();

    QJsonObject dataObject;
    dataObject.insert("UserMsg", QString(m_UserMsg.toUtf8().toBase64()));
    dataObject.insert("UserMessageType", (int)m_UserMessageType);

    rootObject.insert("MessageData", dataObject);

    return rootObject;
}
